package com.heima.xiyuanmall.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Typeface;
import android.os.Parcel;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;

import androidx.annotation.Nullable;

import com.heima.xiyuanmall.listener_adapter.AdapterAnimationListener;

import java.util.Random;

public class LaunchTextView extends View {
    public LaunchTextView(Context context) {
        super(context);
    }

    public LaunchTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public LaunchTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private int color=Color.WHITE;
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //view控件正方形边长
        float side = Math.min(getWidth(), getHeight());
        Log.i("TAG", "onDraw: "+getWidth()+"      "+getHeight());
        canvas.drawColor(Color.BLACK);
        TextPaint textPaint = new TextPaint();
        textPaint.setColor(color);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setTextSize(side/2/1.5f);
        textPaint.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/HYShangWeiShouShuW.ttf"));

        canvas.drawText("西",side/2/2/2.1f,side/2,textPaint);
        canvas.drawText("猿",side/2+side/2/10,side/2,textPaint);

    }



    public void startAnimation(){
        AlphaAnimation anim = new AlphaAnimation(0,1);
        anim.setDuration(2000);
        anim.setFillAfter(true);
        anim.start();
        this.startAnimation(anim);
        TranslateAnimation[] translateAnimations=addTranslateAnimations(10000);

        anim.setAnimationListener(new AdapterAnimationListener() {
            @Override
            public void onAnimationEnd(Animation animation) {
                LaunchTextView.this.startAnimation(translateAnimations[0]);
                translateAnimations[0].start();
                LaunchTextView.this.invalidate();
            }
        });


    }

    /**
     *  @author Jins
     *  @date created at：2021/11/21
     *  @Description: 动画个数
     */
    private TranslateAnimation[] addTranslateAnimations(int length){
        Random random = new Random();
        int[] colors = new int[]{Color.BLACK,
                Color.DKGRAY,
                Color.GRAY,
                Color.LTGRAY,
                Color.WHITE,
                Color.RED,
                Color.GREEN,
                Color.BLUE,
                Color.YELLOW,
                Color.CYAN,
                Color.MAGENTA,
                Color.TRANSPARENT};
        TranslateAnimation[] translateAnimations = new TranslateAnimation[length];
        for (int i = 0; i <length ; i++) {
            translateAnimations[i]=new TranslateAnimation(random.nextInt(100)-50,
                    random.nextInt(100)-50,
                    random.nextInt(100)-50,
                    random.nextInt(100)-50);
            translateAnimations[i].setDuration(10);
            int finalI = i;
            translateAnimations[i].setAnimationListener(new AdapterAnimationListener() {
                @Override
                public void onAnimationEnd(Animation animation) {
                    if(length== finalI+1){
                        color = Color.WHITE;
                    }else {
                        color = colors[random.nextInt(12)];
                        LaunchTextView.this.startAnimation(translateAnimations[finalI+1]);
                        translateAnimations[finalI+1].start();
                    }
                    LaunchTextView.this.invalidate();
                }
            });
        }
        return translateAnimations;
    }

}
